// LayParam.cpp: implementation of the CLayParam class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
/*#include "../co2cutsystemhg.h"*/
#include "LayParam.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLayParam::CLayParam()
{
}

CLayParam::~CLayParam()
{
}

int CLayParam::ReadConfig()
{
	CString strLay;
	CString strNum;
	for (int k=0; k<8; k++)
	{
		strNum.Format(_T("%d"),k);
		strLay = _T("Lay")+strNum;
		ReadKey(strLay,_T("Max"),m_layNO[k].m_dMaxV);
		ReadKey(strLay,_T("Min"),m_layNO[k].m_dMinV);
		ReadKey(strLay,_T("Acc"),m_layNO[k].m_dAcc);		
		ReadKey(strLay,_T("AccCorner"),m_layNO[k].m_dAccCorner);		
		ReadKey(strLay,_T("Power"),m_layNO[k].m_dPower);		
		ReadKey(strLay,_T("Frequence"),m_layNO[k].m_dFrequence);		
		ReadKey(strLay,_T("Duty"),m_layNO[k].m_dDuty);		
		ReadKey(strLay,_T("LeadInOffset"),m_layNO[k].m_LeadInOffset);		
		ReadKey(strLay,_T("LeadOutOffset"),m_layNO[k].m_LeadOutOffset);
		ReadKey(strLay,_T("MaxMarkZ"),m_layNO[k].m_maxMarkZ);
		ReadKey(strLay,_T("MinMarkZ"),m_layNO[k].m_minMarkZ);
		ReadKey(strLay,_T("AccMarkZ"),m_layNO[k].m_AccMarkZ);
		ReadKey(strLay,_T("LaserOnDelay"),m_layNO[k].m_LaserOnDelay);
		ReadKey(strLay,_T("LaserOffDelay"),m_layNO[k].m_LaserOffDelay);
		ReadKey(strLay,_T("EnableLay"),m_layNO[k].m_iEnable);
		ReadKey(strLay,_T("GasOnDelay"),m_layNO[k].m_GasOnDelay);
		ReadKey(strLay,_T("GasOffDelay"),m_layNO[k].m_GasOffDelay);
		ReadKey(strLay,_T("EmptyMaxMove"),m_layNO[k].m_dEmptyMaxSpeed);
// 		ReadKey(strLay,_T("EmptyMinMove"),m_layNO[k].m_dEmptyBeginSpeed);
 		ReadKey(strLay,_T("EmptyAccMove"),m_layNO[k].m_dEmptyAccSpeed);
		ReadKey(strLay,_T("JumpCornerDelay"),m_layNO[k].m_JumpCornerDelay);
	}
	return 0;
}

int CLayParam::WriteConfig()
{
	CString strLay;
	CString strNum;
	for (int k=0; k<8; k++)
	{
		strNum.Format(_T("%d"),k);
		strLay = _T("Lay")+strNum;
		WriteKey(strLay,_T("Max"),m_layNO[k].m_dMaxV);
		WriteKey(strLay,_T("Min"),m_layNO[k].m_dMinV);
		WriteKey(strLay,_T("Acc"),m_layNO[k].m_dAcc);		
		WriteKey(strLay,_T("AccCorner"),m_layNO[k].m_dAccCorner);		
		WriteKey(strLay,_T("Power"),m_layNO[k].m_dPower);		
		WriteKey(strLay,_T("Frequence"),m_layNO[k].m_dFrequence);		
		WriteKey(strLay,_T("Duty"),m_layNO[k].m_dDuty);		
		WriteKey(strLay,_T("LeadInOffset"),m_layNO[k].m_LeadInOffset);		
		WriteKey(strLay,_T("LeadOutOffset"),m_layNO[k].m_LeadOutOffset);
		WriteKey(strLay,_T("MaxMarkZ"),m_layNO[k].m_maxMarkZ);
		WriteKey(strLay,_T("MinMarkZ"),m_layNO[k].m_minMarkZ);
		WriteKey(strLay,_T("AccMarkZ"),m_layNO[k].m_AccMarkZ);
		WriteKey(strLay,_T("LaserOnDelay"),m_layNO[k].m_LaserOnDelay);
		WriteKey(strLay,_T("LaserOffDelay"),m_layNO[k].m_LaserOffDelay);
		WriteKey(strLay,_T("EnableLay"),m_layNO[k].m_iEnable);
		WriteKey(strLay,_T("GasOnDelay"),m_layNO[k].m_GasOnDelay);
		WriteKey(strLay,_T("GasOffDelay"),m_layNO[k].m_GasOffDelay);
		WriteKey(strLay,_T("EmptyMaxMove"),m_layNO[k].m_dEmptyMaxSpeed);
// 		WriteKey(strLay,_T("EmptyMinMove"),m_layNO[k].m_dEmptyBeginSpeed);
 		WriteKey(strLay,_T("EmptyAccMove"),m_layNO[k].m_dEmptyAccSpeed);
		WriteKey(strLay,_T("JumpCornerDelay"),m_layNO[k].m_JumpCornerDelay);
	}
	return 0;
}

int CLayParam::OpenFile(CStdioFileEx* pFile)
{
	for (int k=0; k<8; k++)
	{
		CString strStringData;
		while (pFile->ReadString(strStringData))
		{
			if (strStringData == _T("</LayParam>"))
			{
				break;
			}
			else
			{
				CString strData,strName;
				CTool::GetDataAndNameFromCUT(strStringData, strData,strName);
				
				CString strNum;
				strNum.Format(_T("%d"),k);
				if (strName == (_T("MaxV")+strNum))
				{
					m_layNO[k].m_dMaxV = CTool::CStringToDouble(strData);
				}
				else if (strName == (_T("MinV")+strNum))
				{
					m_layNO[k].m_dMinV = CTool::CStringToDouble(strData);
				}
				else if (strName == (_T("Acc")+strNum))
				{
					m_layNO[k].m_dAcc = CTool::CStringToDouble(strData);
				}
				else if (strName == (_T("AccCorner")+strNum))
				{
					m_layNO[k].m_dAccCorner = CTool::CStringToDouble(strData);
				}
				else if (strName == (_T("Frequence")+strNum))
				{
					m_layNO[k].m_dFrequence = CTool::CStringToDouble(strData);
				}
				else if (strName == (_T("Duty")+strNum))
				{
					m_layNO[k].m_dDuty = CTool::CStringToDouble(strData);
				}
				else if (strName == (_T("Power")+strNum))
				{
					m_layNO[k].m_dPower = CTool::CStringToDouble(strData);
				}
				else if (strName == (_T("LeadInOffset")+strNum))
				{
					m_layNO[k].m_LeadInOffset = _ttoi(strData);
				}
				else if (strName == (_T("LeadOutOffset")+strNum))
				{
					m_layNO[k].m_LeadOutOffset = _ttoi(strData);
					break;
				}
				else if (strName == (_T("LaserOnDelay")+strNum))
				{
					m_layNO[k].m_LaserOnDelay = CTool::CStringToDouble(strData);
					break;
				}
				else if (strName == (_T("LaserOffDelay")+strNum))
				{
					m_layNO[k].m_LaserOffDelay = CTool::CStringToDouble(strData);
					break;
				}
				else if (strName == (_T("EnableLay")+strNum))
				{
					m_layNO[k].m_iEnable = _ttoi(strData);
					break;
				}
				else if (strName == (_T("GasOnDelay")+strNum))
				{
					m_layNO[k].m_GasOnDelay = CTool::CStringToDouble(strData);
					break;
				}
				else if (strName == (_T("GasOffDelay")+strNum))
				{
					m_layNO[k].m_GasOffDelay = CTool::CStringToDouble(strData);
					break;
				}
				else if (strName == (_T("EmptyMaxMove")+strNum))
				{
					m_layNO[k].m_dEmptyMaxSpeed = CTool::CStringToDouble(strData);
					break;
				}
				else if (strName == (_T("EmptyAccMove")+strNum))
				{
					m_layNO[k].m_dEmptyAccSpeed = CTool::CStringToDouble(strData);
					break;
				}
				else if (strName == (_T("JumpCornerDelay")+strNum))
				{
					m_layNO[k].m_JumpCornerDelay = CTool::CStringToDouble(strData);
					break;
				}
			}
		}
	}
	return 0;
}

int CLayParam::SaveFile(CStdioFileEx* pFile)
{	
	CString strWriteString;
	strWriteString.Format(_T("<LayParam>"));
	CTool::WriteToCutFile(pFile,strWriteString);
	for (int k=0; k<8; k++)
	{
		strWriteString.Format(_T("<MaxV%d>%.5f</MaxV%d>"),k,m_layNO[k].m_dMaxV,k);
		CTool::WriteToCutFile(pFile,strWriteString);
		
		strWriteString.Format(_T("<MinV%d>%.5f</MinV%d>"),k,m_layNO[k].m_dMinV,k);
		CTool::WriteToCutFile(pFile,strWriteString);
		
		strWriteString.Format(_T("<Acc%d>%.5f</Acc%d>"),k,m_layNO[k].m_dAcc,k);
		CTool::WriteToCutFile(pFile,strWriteString);
		
		strWriteString.Format(_T("<AccCorner%d>%.5f</AccCorner%d>"),k,m_layNO[k].m_dAccCorner,k);
		CTool::WriteToCutFile(pFile,strWriteString);
		
		strWriteString.Format(_T("<Frequence%d>%.5f</Frequence%d>"),k,m_layNO[k].m_dFrequence,k);
		CTool::WriteToCutFile(pFile,strWriteString);
		
		strWriteString.Format(_T("<Duty%d>%.5f</Duty%d>"),k,m_layNO[k].m_dDuty,k);
		CTool::WriteToCutFile(pFile,strWriteString);
		
		strWriteString.Format(_T("<Power%d>%.5f</Power%d>"),k,m_layNO[k].m_dPower,k);
		CTool::WriteToCutFile(pFile,strWriteString);
		
		strWriteString.Format(_T("<LeadInOffset%d>%d</LeadInOffset%d>"),k,m_layNO[k].m_LeadInOffset,k);
		CTool::WriteToCutFile(pFile,strWriteString);
		
		strWriteString.Format(_T("<LeadOutOffset%d>%d</LeadOutOffset%d>"),k,m_layNO[k].m_LeadOutOffset,k);
		CTool::WriteToCutFile(pFile,strWriteString);

		strWriteString.Format(_T("<LaserOnDelay%d>%.4f</LaserOnDelay%d>"),k,m_layNO[k].m_LaserOnDelay,k);
		CTool::WriteToCutFile(pFile,strWriteString);

		strWriteString.Format(_T("<LaserOffDelay%d>%.4f</LaserOffDelay%d>"),k,m_layNO[k].m_LaserOffDelay,k);
		CTool::WriteToCutFile(pFile,strWriteString);
		
		strWriteString.Format(_T("<EnableLay%d>%d</EnableLay%d>"),k,m_layNO[k].m_iEnable,k);
		CTool::WriteToCutFile(pFile,strWriteString);

		strWriteString.Format(_T("<GasOnDelay%d>%.4f</GasOnDelay%d>"),k,m_layNO[k].m_GasOnDelay,k);
		CTool::WriteToCutFile(pFile,strWriteString);

		strWriteString.Format(_T("<GasOffDelay%d>%.4f</GasOffDelay%d>"),k,m_layNO[k].m_GasOffDelay,k);
		CTool::WriteToCutFile(pFile,strWriteString);

		strWriteString.Format(_T("<EmptyMaxMove%d>%.4f</EmptyMaxMove%d>"),k,m_layNO[k].m_dEmptyMaxSpeed,k);
		CTool::WriteToCutFile(pFile,strWriteString);

		strWriteString.Format(_T("<EmptyAccMove%d>%.4f</EmptyAccMove%d>"),k,m_layNO[k].m_dEmptyAccSpeed,k);
		CTool::WriteToCutFile(pFile,strWriteString);

		strWriteString.Format(_T("<JumpCornerDelay%d>%.4f</JumpCornerDelay%d>"),k,m_layNO[k].m_JumpCornerDelay,k);
		CTool::WriteToCutFile(pFile,strWriteString);
	}
	strWriteString.Format(_T("</LayParam>"));
	CTool::WriteToCutFile(pFile,strWriteString);
	return 0;
}

int CLayParam::operator=(const CLayParam& theLayParam)
{
	for (int k=0; k<8; k++)
	{
		m_layNO[k].m_dMaxV = theLayParam.m_layNO[k].m_dMaxV;
		m_layNO[k].m_dMinV = theLayParam.m_layNO[k].m_dMinV;
		m_layNO[k].m_dAcc = theLayParam.m_layNO[k].m_dAcc;
		m_layNO[k].m_dAccCorner = theLayParam.m_layNO[k].m_dAccCorner;
		m_layNO[k].m_dPower = theLayParam.m_layNO[k].m_dPower;
		m_layNO[k].m_dFrequence = theLayParam.m_layNO[k].m_dFrequence;
		m_layNO[k].m_dDuty = theLayParam.m_layNO[k].m_dDuty;
		m_layNO[k].m_LeadInOffset = theLayParam.m_layNO[k].m_LeadInOffset;
		m_layNO[k].m_LeadOutOffset = theLayParam.m_layNO[k].m_LeadOutOffset;
		m_layNO[k].m_maxMarkZ = theLayParam.m_layNO[k].m_maxMarkZ;
		m_layNO[k].m_minMarkZ = theLayParam.m_layNO[k].m_minMarkZ;
		m_layNO[k].m_AccMarkZ = theLayParam.m_layNO[k].m_AccMarkZ;
		m_layNO[k].m_LaserOnDelay = theLayParam.m_layNO[k].m_LaserOnDelay;
		m_layNO[k].m_LaserOffDelay = theLayParam.m_layNO[k].m_LaserOffDelay;
		m_layNO[k].m_iEnable = theLayParam.m_layNO[k].m_iEnable;
		m_layNO[k].m_GasOnDelay = theLayParam.m_layNO[k].m_GasOnDelay;
		m_layNO[k].m_GasOffDelay = theLayParam.m_layNO[k].m_GasOffDelay;
		m_layNO[k].m_dEmptyMaxSpeed = theLayParam.m_layNO[k].m_dEmptyMaxSpeed;
// 		m_layNO[k].m_dEmptyBeginSpeed = theLayParam.m_layNO[k].m_dEmptyBeginSpeed;
 		m_layNO[k].m_dEmptyAccSpeed = theLayParam.m_layNO[k].m_dEmptyAccSpeed;
		m_layNO[k].m_JumpCornerDelay = theLayParam.m_layNO[k].m_JumpCornerDelay;
	}
	return 0;
}